"""
Case Type   : dolphin
Case Name   : 修改dolphin.so文件权限后，查询插件是否自动加载
Create At   : 2022.04.06
Owner       : z@wan005
Description :
    1、下载插件所需文件至对应目录
    2、创建b库
    3、修改dolphin.so文件权限为000
    4、连接b库查询插件是否存在
    5、恢复dolphin.so文件权限为755
    6、b库下查询插件
    7、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、报错，权限拒绝
    5、成功
    6、自动加载成功,无需手动创建
    7、清理环境完成
History     :modified by z@wan005,2022.08.29，om数据库目前实现自动加载dolphin插件，
             因此删除用例中拷贝插件依赖文件步骤
             modified by zhanghuan96,2024.7.4，添加等待时间
"""

import os
import time
import unittest
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class PluginTest020(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_plugin0020'
        self.part_path = os.path.dirname(macro.DB_INSTANCE_PATH)
        self.put_path1 = os.path.join(self.part_path, 'app', 'lib',
                                      'postgresql')

    def test_dolphin(self):
        text = '----step1:创建b库;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} DBCOMPATIBILITY ='b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)

        text = '-----step3:修改dolphin.so文件权限为000;expect:成功---'
        self.log.info(text)
        chmod_res = self.user_node.sh(
            f'chmod 000 {self.put_path1}/dolphin.so').result()
        self.log.info(chmod_res)
        self.assertEqual('', chmod_res, '执行失败:' + text)

        self.log.info('重启数据库')
        self.sh_primy.restart_db_cluster()
        result = self.sh_primy.get_db_cluster_status()
        self.assertTrue("Degraded" in result or "Normal" in result)

        text = '--step4:连接b库查询插件是否存在;expect:报错，权限拒绝---'
        self.log.info(text)
        sql_cmd = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn('Permission denied', sql_res, '执行失败' + text)

        text = '-----step5:恢复dolphin.so文件权限为755;expect:成功---'
        self.log.info(text)
        chmod_res = self.user_node.sh(
            f'chmod 755 {self.put_path1}/dolphin.so').result()
        self.log.info(chmod_res)
        self.assertEqual('', chmod_res, '执行失败:' + text)
        time.sleep(15)
        self.log.info('重启数据库')
        self.sh_primy.restart_db_cluster()
        result = self.sh_primy.get_db_cluster_status()
        self.assertTrue("Degraded" in result or "Normal" in result)

        text = '--step6:b库下查询插件;expect:自动加载成功,无需手动创建---'
        self.log.info(text)
        sql_cmd = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertEqual('dolphin', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

    def tearDown(self):
        text = '--step7:清理环境;expect:清理环境完成--'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};"
        sql_res = self.sh_primy.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.log.info('断言teardown成功')
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
